package group.igfd.manager.biz.controller;

import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import group.igfd.manager.biz.entity.PublishExpertsQuery;
import group.igfd.manager.biz.entity.ReviewReasonEntity;
import group.igfd.manager.biz.entity.UsersEntity;
import group.igfd.manager.biz.model.GoodDetail;
import group.igfd.manager.biz.service.ReviewReasonService;
import group.igfd.manager.biz.service.UsersService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import group.igfd.manager.biz.entity.PublishExpertsEntity;
import group.igfd.manager.biz.service.PublishExpertsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 达人发布表
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-12-13 11:43:17
 */
@RestController
@RequestMapping("biz/publishExperts")
public class PublishExpertsController {
    @Autowired
    private PublishExpertsService publishExpertsService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private ReviewReasonService reviewReasonService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("biz:publishExperts:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = publishExpertsService.queryPage(params);
        List<PublishExpertsEntity> list = (List<PublishExpertsEntity>) page.getList();

        List<PublishExpertsQuery> resultPublishExpertsList = new ArrayList<>();
        for (PublishExpertsEntity publishExpertsEntity : list) {
            PublishExpertsQuery publishExpertsQuery = new PublishExpertsQuery();
            try {
                BeanUtils.copyProperties(publishExpertsQuery, publishExpertsEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }

            EntityWrapper<UsersEntity> usersEntityEntityWrapper = new EntityWrapper<>();
            usersEntityEntityWrapper.eq("uid",publishExpertsEntity.getUid());
            UsersEntity usersEntity = usersService.selectOne(usersEntityEntityWrapper);
            if (usersEntity != null) {
                publishExpertsQuery.setPublishName(usersEntity.getNickname());
            }

            resultPublishExpertsList.add(publishExpertsQuery);
        }
        page.setList(resultPublishExpertsList);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:publishExperts:info")
    public R info(@PathVariable("id") Integer id){
        Map<String, Object> resultMap = new HashMap<>();
        PublishExpertsEntity publishExperts = publishExpertsService.selectById(id);

        if (publishExperts != null) {
            resultMap.put("id",publishExperts.getId());
            resultMap.put("article",publishExperts.getArticle());

            List<GoodDetail> detailDescObj = JSON.parseObject(publishExperts.getDescJson(), new TypeReference<List<GoodDetail>>() {
            });

            List<Map> imageArr = new ArrayList<>();
            if (StringUtils.isNotEmpty(publishExperts.getTitlePicture())) {
                Map<String, String> imageInfo = new HashMap<>();
                imageInfo.put("src",publishExperts.getTitlePicture());
                imageArr.add(imageInfo);
            }


            for (GoodDetail goodDetail : detailDescObj) {
                Map<String, String> imageInfo = new HashMap<>();
                imageInfo.put("src",goodDetail.getImageURL());

                imageArr.add(imageInfo);
            }

            resultMap.put("images",imageArr);
        }

        return R.ok().put("publishExperts", resultMap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:publishExperts:save")
    public R save(@RequestBody PublishExpertsEntity publishExperts){
			publishExpertsService.insert(publishExperts);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:publishExperts:update")
    public R update(@RequestBody PublishExpertsEntity publishExperts){
			publishExpertsService.updateById(publishExperts);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:publishExperts:delete")
    public R delete(@RequestBody Integer[] ids){
			publishExpertsService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 审核
     * */
    @RequestMapping("/auth/{id}")
    @RequiresPermissions("biz:publishExperts:update")
    public R auth(@PathVariable("id") Integer id,@RequestParam Integer state,
                  @RequestParam(required = false) String reason){
        ReviewReasonEntity reviewReasonEntity = null;
        if (state == 2) {
            reviewReasonEntity = new ReviewReasonEntity();

            reviewReasonEntity.setParentId(id);
            reviewReasonEntity.setType(1);
            reviewReasonEntity.setReason(reason);
            reviewReasonEntity.setCreatedAt(new Date());
            reviewReasonEntity.setUpdatedAt(new Date());

            reviewReasonService.insert(reviewReasonEntity);
        }

        PublishExpertsEntity publishExpertsEntity = new PublishExpertsEntity();
        publishExpertsEntity.setId(id);
        publishExpertsEntity.setCheckFlag(state);
        publishExpertsEntity.setUpdatedAt(new Date());
        if (state == 2 && reviewReasonEntity != null) {
            publishExpertsEntity.setReasonId(reviewReasonEntity.getId());
        }

        publishExpertsService.updateById(publishExpertsEntity);

        return R.ok();
    }

}
